package meiyunting.shiwuzhaoling_gatway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.ApplicationRunner;

@SpringBootApplication
@EnableDiscoveryClient  // 启用服务发现
public class ShiwuzhaolingGatwayApplication {

    public static void main(String[] args) {
        // 设置Zipkin相关系统属性，避免异常
        System.setProperty("zipkin.collector.http.enabled", "true");
        System.setProperty("spring.zipkin.enabled", "true");
        System.setProperty("spring.sleuth.enabled", "true");
        System.setProperty("spring.zipkin.service.name", "shiwuzhaoling-gateway");
        
        SpringApplication.run(ShiwuzhaolingGatwayApplication.class, args);
    }
    
    /**
     * 应用启动后执行的初始化操作
     */
    @Bean
    public ApplicationRunner applicationRunner() {
        return args -> {
            // 应用启动后的初始化操作
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                // 应用关闭时的清理操作
                try {
                    // 给Zipkin发送器一些时间完成发送
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }));
        };
    }
}
